package cn.stylefeng.guns.modular.system.dto;

import com.alibaba.fastjson.JSONObject;
import com.nineshadow.games.Enum.GameTypeEnum;
import lombok.Data;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

@Data
public class ResourceUnionRequestDto<T> {

    private String appId;

    private long time;

    private T body;

    private String sig;

    public ResourceUnionRequestDto(GameTypeEnum gameTypeEnum, T body) {
        this.appId = gameTypeEnum.getAppId();
        this.time = System.currentTimeMillis()/1000;
        this.body = body;
        this.sig = generateSign(gameTypeEnum.getAppSecret());
    }

    private String generateSign(String appSecret) {
        String stringA= "appId="+appId+"&body="+ JSONObject.toJSONString(body)+"&time="+time;
        String stemp = stringA + appSecret+ appId;
        String s = DigestUtils.md5DigestAsHex(stemp.getBytes(StandardCharsets.UTF_8));
        return s.toLowerCase();
    }
}
